package com.ityunhe.sevlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import com.ityunhe.bean.Garden;
import com.ityunhe.bean.Goods;
import com.ityunhe.bean.Goodsclasses;
import com.ityunhe.bean.Page;
import com.ityunhe.biz.GoodsShowBis;
@WebServlet("/home/GoodsShowServlet")
public class GoodsShowServlet extends HttpServlet  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		// TODO Auto-generated method stub
			String opr = request.getParameter("opr");
			GoodsShowBis goodsShowBis = new GoodsShowBis();
			if("first".equals(opr)){
				//得到二级分类信息 例如番茄
				
				
				List<Goodsclasses> goodClasses = goodsShowBis.getClasses(1);
				//得到商家信息
				List<Garden> gardenList = goodsShowBis.selectGarden();
				
				//默认分页
					Page<Goods> pa = new Page<Goods>();	
					Map <String, Object>map = new HashMap<String, Object>();
					pa = goodsShowBis.selectPage(pa, map);
					
					
					request.setAttribute("goodClasses", goodClasses);
					request.setAttribute("gardenList", gardenList);
					request.setAttribute("page", pa);
					request.getRequestDispatcher("/home/search.jsp").forward(request, response);
				
		/*	JSONObject json=new JSONObject();
			List<JSONObject> jsonList=new ArrayList<JSONObject>();*/
				/*for (Goodsclasses goodsclasses : classesTwo) {
					jsonList.add(new JSONObject(goodsclasses));
				}
				try {
					json.put("goodsList",jsonList);
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}	*/
				
			/*	jsonList.clear();
				for (Garden garden : gardenList) {
					jsonList.add(new JSONObject(garden));
				}
				try {
					json.put("gardenList", jsonList);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	*/
				
				//默认分页
			/*	Page<Goods> pa = new Page<Goods>();	
				pa.setPageIndex(0);
				pa.setPageSize(12);
				Map <String, Object>map = new HashMap<String, Object>();
				//map.put("gaid", );
				//map.put("gsid", );
				pa = goodsShowBis.selectPage(pa, map);
				jsonList.clear();
				for (Map goods : pa.getList()) {
					jsonList.add(new JSONObject(goods));
				}
				
				try {
					json.put("goodsPage", jsonList);
					System.out.println(json);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				PrintWriter writer = response.getWriter();
				writer.print(json);*/
			}

			if("pageListajax".equals(opr)){
				
				String gaid = request.getParameter("gaid");
				Page<Map> page = new Page<Map>();	
				Map<String, Object> map =new HashMap<String, Object>();
				HttpSession session = request.getSession();
				
				
				if(!"".equals(gaid)&&gaid!=null){
					session.setAttribute("gaid", gaid);
				}
				map.put("gaid", session.getAttribute("gaid"));
				//得到集合
				page = goodsShowBis.selectPage(page, map);
				
				//创建json对象
				JSONObject json=new JSONObject();
				
				List<JSONObject> jsonList=new ArrayList<JSONObject>();
				
				for (Map goods : page.getList()) {
					jsonList.add(new JSONObject(goods));
				}
				
				try {
					json.put("goodsPage", jsonList);
					System.out.println(json);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
				PrintWriter writer = response.getWriter();
				writer.print(json);
				
			}
			
			
			
				
					if("pageList".equals(opr)){
						System.out.println("doget");
						
						List<Goodsclasses> goodClasses = goodsShowBis.getClasses(1);
						//得到商家信息
						List<Garden> gardenList = goodsShowBis.selectGarden();
						

						request.setAttribute("goodClasses", goodClasses);
						request.setAttribute("gardenList", gardenList);
						
						
					
						
						
						
						
						
						String gaid = request.getParameter("gaid");
						String gsid = request.getParameter("gsid");	
						String sales = request.getParameter("sales");
						String price = request.getParameter("price");
						String pageIndex=request.getParameter("pageIndex");		
						
						
						Page<Goods> page = new Page<Goods>();	
						Map<String, Object> map =new HashMap<String, Object>();
						HttpSession session = request.getSession();
						
						if(!"".equals(sales)&&sales!=null){
							if("desc".equals((String)session.getAttribute("sales"))){
								session.setAttribute("sales", "asc");
								session.removeAttribute("price");
							}else{
								session.setAttribute("sales", "desc");	
								session.removeAttribute("price");
							}	
						}
						if(!"".equals(pageIndex)&&pageIndex!=null){
							page.setPageIndex(Integer.parseInt(pageIndex));
						}
						
						
						
						
						if(!"".equals(price)&&price!=null){
							if("desc".equals((String)session.getAttribute("price"))){
								session.setAttribute("price", "asc");
								session.removeAttribute("sales");
							}else{
								session.setAttribute("price", "desc");	
								session.removeAttribute("sales");
							}	
						}
						
						
						if(!"".equals(gaid)&&gaid!=null){
							session.setAttribute("gaid", gaid);
						}
						
						
						if(!"".equals(gaid)&&gaid!=null){
							session.setAttribute("gaid", gaid);
						}
						if(!"".equals(gsid)&&gsid!=null){
							session.setAttribute("gsid", gsid);
						}
						
						if(pageIndex!=null){				
							page.setPageIndex(Integer.parseInt(pageIndex));
						}
						if("all".equals(gaid)){
							session.removeAttribute("gaid");
						}
						if("all".equals(gsid)){
							session.removeAttribute("gsid");
						}
					
						map.put("gaid", session.getAttribute("gaid"));
						map.put("gsid", session.getAttribute("gsid"));
						map.put("price", session.getAttribute("price"));
						map.put("sales", session.getAttribute("sales"));
						
						page = goodsShowBis.selectPage(page, map);
						request.setAttribute("page",page);
						request.setAttribute("gaid", gaid);		
						request.setAttribute("gsid", gsid);
						request.getRequestDispatcher("/home/search.jsp").forward(request, response);
					}
					
			
		
	}
	
	
	

}
